<?php
/**
 * Created by PhpStorm
 * User: hean
 * Date: 2017/10/31
 * Time: 9:05
 */

namespace app\index\controller;

use think\Controller;
use think\Cookie;
use think\Session;
use think\Request;
use app\index\model\Goods as GoodsModel;
use app\index\model\Attr as AttrModel;
use app\index\model\Brand as BrandModel;
use app\index\model\Type as TypeModel;
use app\index\model\Cart as CartModel;

class Index extends Controller
{

	//首页信息展示--------------------------------------------------------
	public function index()
	{

		//判断是否登录---------
		if (Cookie::get('loginBy')) {

			if (Cookie::get('loginBy') == 'email') {
				$data['indexname'] = Cookie::get('email');
			} else if (Cookie::get('loginBy') == 'tel') {
				$data['indexname'] = Cookie::get('tel');
			} else if (Cookie::get('loginBy') == 'username') {
				$data['indexname'] = Cookie::get('username');
			}
			$this->assign('logindata', $data);

			//购物车信息
			$uid = Cookie::get('uid');
			$cart = CartModel::where(['uid'=>$uid])->select();
			$cart = count($cart);
			$this->assign('cartNum', $cart);
		}

		//查询首页商品信息----------
		$result = GoodsModel::field('goods_id, goods_name, shop_price, mid_logo')->where(['is_on_sale' => 1, 'goods_del' => 0])->select();
		$this->assign('goodsData', $result);

		return $this->fetch();
	}

	//商品详情页------------------------------------------------------
	public function introduction(Request $request)
	{
		//判断是否登录----------
		if (Cookie::get('loginBy')) {

			if (Cookie::get('loginBy') == 'email') {
				$data['indexname'] = Cookie::get('email');
			} else if (Cookie::get('loginBy') == 'tel') {
				$data['indexname'] = Cookie::get('tel');
			} else if (Cookie::get('loginBy') == 'username') {
				$data['indexname'] = Cookie::get('username');
			}
			$this->assign('logindata', $data);
		}

		//查询商品的详情-------------
		$gid = $request->get('gid');
		$goodsInfo = GoodsModel::get(['goods_id' => $gid]);
		$infoData['goods_id'] = $goodsInfo->goods_id;
		$infoData['goods_name'] = $goodsInfo->goods_name;
		$infoData['goods_card'] = $goodsInfo->goods_card;
		$attrId = $goodsInfo->attr_id;
		$attrData = AttrModel::where(['attr_id' => $attrId])->select();
		$brandId = $goodsInfo->brand_id;
		$brandData = BrandModel::get(['brand_id' => $brandId]);
		$typeId = $goodsInfo->type_id;
		$typeData = TypeModel::get(['type_id' => $typeId]);
		$infoData['logo'] = $goodsInfo->logo;
		$infoData['big_logo'] = $goodsInfo->big_logo;
		$infoData['mid_logo'] = $goodsInfo->mid_logo;
		$infoData['small_logo'] = $goodsInfo->small_logo;
		$infoData['market_price'] = $goodsInfo->market_price;
		$infoData['shop_price'] = $goodsInfo->shop_price;
		$infoData['goods_desc'] = $goodsInfo->goods_desc;
		$infoData['weight'] = $goodsInfo->weight;
		$infoData['weight_unit'] = $goodsInfo->weight_unit;
		$infoData['stock_num'] = $goodsInfo->stock_num;
		$infoData['sale_num'] = $goodsInfo->sale_num;
		$this->assign('goodInfo', $infoData);
		$this->assign('attrData', $attrData);
		$this->assign('brandData', $brandData);
		$this->assign('typeData', $typeData);

		return $this->fetch();
	}

	//退出登录
	public function loginout()
	{
		Cookie::set('email', '');
		Cookie::set('tel', '');
		Cookie::set('username', '');
		echo "<script>window.location.href='/index/index/index'</script>";
	}
}
